package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/28 15:58
 */
@RestController
@Api(tags = "C端地址簿相关接口")
@Slf4j
@RequestMapping(value = "/user/addressBook")
public class AddressBookController {
    @Resource
    private AddressBookService addressBookService;

    /**
     * 增加地址簿
     *
     * @param addressBook
     * @return
     */
    @ApiOperation(value = "增加地址簿")
    @PostMapping
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("addressBook = {}", addressBook);
        addressBookService.addAddressBook(addressBook);
        return Result.success();
    }


    /**
     * 查询当前用户的所有地址簿
     *
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "查询当前用户的所有地址簿")
    public Result<List<AddressBook>> showAddressBook() {
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> list = addressBookService.getAllAddressBookByUserId(userId);
        return Result.success(list);
    }

    /**
     * 根据id查询地址簿
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询地址簿")
    @GetMapping(value = "/{id}")
    public Result<AddressBook> getById(@PathVariable(value = "id") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 修改地址簿
     *
     * @return
     */
    @ApiOperation(value = "修改地址簿")
    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     *
     * @return
     */
    @ApiOperation(value = "设置默认地址")
    @PutMapping(value = "/default")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook) {
        log.info("id = {}", addressBook.getId());
        addressBookService.setDefaultAddressById(addressBook.getId());
        return Result.success();
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @ApiOperation(value = "查询默认地址")
    @GetMapping(value = "/default")
    public Result<AddressBook> getDefaultAddress() {
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook1 = AddressBook.builder()
                .userId(userId)
                .isDefault(1).build();
        AddressBook addressBook = addressBookService.getDefaultAddress(addressBook1);
        return Result.success(addressBook);
    }

    /**
     * 根据id删除地址
     *
     * @return
     */
    @ApiOperation(value = "根据id删除地址")
    @DeleteMapping
    public Result delete(Long id) {
        addressBookService.deleteAddress(id);
        return Result.success();
    }
}
